package jun.carlauncher;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import java.util.ArrayList;
import java.util.List;
import jun.carlauncher.base.BaseActivity;
import jun.carlauncher.utils.AppUtils;
import org.greenrobot.eventbus.EventBus;

public class MainActivity extends BaseActivity implements View.OnClickListener {

  @BindView(R.id.tv_time) TextView tvTime;
  @BindView(R.id.tv_data) TextView tvData;
  @BindView(R.id.tv_week) TextView tvWeek;
  private ResolveInfo appInfo[] = new ResolveInfo[5];

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    findViewById(R.id.appButton1).setOnClickListener(this);
    findViewById(R.id.appButton2).setOnClickListener(this);
    findViewById(R.id.appButton3).setOnClickListener(this);
    findViewById(R.id.appButton4).setOnClickListener(this);
    findViewById(R.id.appButton5).setOnClickListener(this);
    appInfo[0] = getPackageName(Constants.appStr1);
    appInfo[1] = getPackageName(Constants.appStr2);
    appInfo[2] = getPackageName(Constants.appStr3);
    appInfo[3] = getPackageName(Constants.appStr4);
    appInfo[4] = getPackageName(Constants.appStr5);

    appInfoList = new ArrayList<>();

    timeHandler.sendEmptyMessage(1);
  }

  @Override public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.appButton1) {
      startApplication(appInfo[0]);
    } else if (id == R.id.appButton2) {
      startApplication(appInfo[1]);
    } else if (id == R.id.appButton3) {
      startApplication(appInfo[2]);
    } else if (id == R.id.appButton4) {
      startApplication(appInfo[3]);
    } else if (id == R.id.appButton5) {
      EventBus.getDefault().postSticky(appInfoList);
      skipIntent(AppListActivity.class);
    }
  }

  private void startApplication(ResolveInfo resolveinfo) {
    if (resolveinfo != null) {
      String packageName = resolveinfo.activityInfo.packageName;
      String className = resolveinfo.activityInfo.name;
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      ComponentName cn = new ComponentName(packageName, className);
      intent.setComponent(cn);
      startActivity(intent);
    }
  }

  //启动外部APP
  private ResolveInfo getPackageName(String packagename) {
    PackageInfo packageinfo = null;
    try {
      packageinfo = getPackageManager().getPackageInfo(packagename, 0);
    } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
    }
    if (packageinfo == null) {
      return null;
    }

    Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
    resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resolveIntent.setPackage(packageinfo.packageName);
    List<ResolveInfo> resolveinfoList = getPackageManager().queryIntentActivities(resolveIntent, 0);
    return resolveinfoList.iterator().next();
  }

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
    return false;
  }

  @Override public void onStart() {
    super.onStart();
    new GetAPPListTask().execute();
  }

  List<AppUtils.AppInfo> appInfoList;

  @SuppressLint("StaticFieldLeak") private class GetAPPListTask
      extends AsyncTask<String, Object, List<AppUtils.AppInfo>> {
    @Override protected List<AppUtils.AppInfo> doInBackground(String... strings) {
      appInfoList = AppUtils.getAppsInfo();
      return appInfoList;
    }
  }

  @SuppressLint("HandlerLeak") private Handler timeHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
      long sysTime = System.currentTimeMillis();
      CharSequence sysTimeStr = DateFormat.format("HH:mm:ss", sysTime);
      CharSequence data =  DateFormat.format("yyyy-MM-dd",sysTime);
      CharSequence week =  DateFormat.format("EEE",sysTime);
      tvTime.setText(sysTimeStr); //更新时间
      tvData.setText(data);
      tvWeek.setText(week);
      // 每1000毫秒发送一次，也就是每1000毫秒进一次Handler，也就是每1000毫秒执行一次refreshTime函数
      timeHandler.sendEmptyMessageDelayed(1, 1000);
    };
  };
}

